<template>
	<view>
		<view class="tab-title">
			<view class="num1">
				<u-tabs :list="list" :is-scroll="false" bar-width="300" bar-height="4" active-color="#00BBAA " inactive-color="#555" :current="current" @change="change"></u-tabs>
			</view>
			<view class="num2" v-show="current == 0">
				<u-tabs bg-color="#F2F2F2" :list="list2" :is-scroll="false" bar-height="4" active-color="#00BBAA " inactive-color="#555" :current="current2" @change="change2"></u-tabs>
			</view>
		</view>
		<view class="list">
			<view class="item" v-for="(item,index) in lists[currents]" :key="index" @click="goJump(item)">
				<view class="u-flex">
					<image class="goods-img" :src="item.courseCover" v-if="currents === 2"></image>
					<image class="goods-img" :src="item.goodsCover" v-else></image>
					<view class="u-flex-1">
						<view class="title u-line-2" v-if="currents === 2">{{item.courseName}}</view>
						<view class="title u-line-2" v-else>{{item.goodsName}}</view>
						<view class="sku"></view>
						<view class="price u-flex">
							<image src="/static/icon2/ib.png"></image>
							<text>{{item.totalMoney}}</text>
						</view>
					</view>
				</view>
				<view class="more u-flex" v-if="currents === 0">
					<view @click.stop="confirm(item.id)">确认收货</view>
				</view>
			</view>
		</view>
		
		<view class="safe-area-inset-bottom"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [{
					name: '商品订单'
				}, {
					name: '课程订单'
				}],
				list2: [{
					name: '待收货'
				}, {
					name: '已收货'
				}],
				current: 0,
				current2:0,
				
				currents:0,//待收货0 已收货1 课程订单2
				
				lists:{'0':[],'1':[],'2':[]},
				page:{'0':1,'1':1,'2':1},
				status:{'0':'loading','1':'loading','2':'loading'},
			}
		},
		methods: {
			change(index) {
				this.current = index;
				if(index === 1){
					// 课程
					this.currents = 2
					this.page[2] = 1
					this.getList()
				}else{
					this.currents = this.current2
					this.page[this.current2] = 1
					this.getList()
				}
			},
			change2(index) {
				this.current2 = index;
				this.currents = index // 0,1
				this.page[index] = 1
				this.getList()
			},
			getList(indexs){
				const index = indexs === undefined ? this.currents : indexs
				this.status[index] = 'loading'
				let data = {
					state:index + 1,
					type:1//商品
				}
				
				if(index === 2){//课程
					data = {
						type:2
					}
				}
				
				this.$u.api.orderList({
					...data,
					page:this.page[index],
					limit:10
				}).then(res=>{
					this.lists[index] = this.page[index] == 1 ? res : this.lists[index].concat(res)
					this.status[index] = res.length  === 10 ? 'loadmore' : 'nomore'
					uni.stopPullDownRefresh()
				})
			},
			confirm(id,index){
				uni.showModal({
					content:'是否确认收到货物',
					success: (res) => {
						res.confirm && this.$u.api.confirm({
							order_id:id
						}).then(res=>{
							this.$u.toast('确认收货成功')
							const i = this.currents
							this.lists[i].splice(index,1)
						})
					}
				})
			},
			goJump(item){
				if(this.currents === 2){
					// 课程详情
					this.$app.jump(`/pages/order/detail-course?id=${item.id}`)
				}else{
					this.$app.jump(`/pages/order/detail-goods?id=${item.id}&show=${this.currents === 0 ? 'show' : 'hide'}`)
				}
			}
		},
		onLoad() {
			this.getList(0)
		},
		onReachBottom() {
			const index = this.currents
			if(this.status[index] == 'loadmore'){
				this.page[index] ++
				this.getList()
			}
		},
		onPullDownRefresh() {
			const index = this.currents
			this.page[index] = 1
			this.getList()
		}
	}
</script>

<style lang="scss">
	page{background-color: #F2F2F2;}
	.tab-title{
		position: sticky;top: 0;z-index: 9;border-top: 1rpx solid #ddd;
		.num1{background-color: #fff;}
	}
	.list{
		padding: 20rpx 50rpx;
		.item{
			background-color: #fff;border-radius: 20rpx;padding: 20rpx;margin-top: 20rpx;
			&:first-child{margin-top: 0;}
			.goods-img{width: 160rpx;height: 160rpx;border-radius: 20rpx;display: block;flex-shrink: 0;margin-right: 22rpx;}
			.title{font-weight: bold;font-size: 29rpx;line-height: 40rpx;height: 80rpx;}
			.sku{font-size: 26rpx;color: #555;padding-bottom: 10rpx;}
			.price{
				image{display: block;width: 36rpx;height: 36rpx;margin-right: 10rpx;flex-shrink: 0;}
				color: #F55332;font-size: 24rpx;font-weight: bold;
			}
		}
		.more{
			justify-content: flex-end;
			view{background-color: #333;color: #fff;border-radius: 100rpx;padding: 4rpx 14rpx;font-size: 28rpx;}
		}
	}
</style>
